package com.zmzncs.lmtc.module.shop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmzncs.lmtc.common.util.GeoHashUtil;
import com.zmzncs.lmtc.module.park.dao.ParkInfoDao;
import com.zmzncs.lmtc.module.park.pojo.park_info.ParkInfoDTO;
import com.zmzncs.lmtc.module.shop.dao.ShopManagerDao;
import com.zmzncs.lmtc.module.shop.dao.ShopManagerShopDao;
import com.zmzncs.lmtc.module.shop.pojo.shop_info.*;
import com.zmzncs.lmtc.module.shop.dao.ShopInfoDao;
import com.zmzncs.lmtc.module.shop.pojo.shop_manager.ShopManager;
import com.zmzncs.lmtc.module.shop.pojo.shop_manager.ShopManagerDTO;
import com.zmzncs.lmtc.module.shop.pojo.shop_manager_shop.ShopManagerShop;
import com.zmzncs.lmtc.module.shop.service.ShopInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aspectj.weaver.StaticJoinPointFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商户-信息 服务实现类
 * </p>
 *
 * @author 高博文
 * @since 2021-05-20
 */
@Service
public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoDao, ShopInfo> implements ShopInfoService {

    @Autowired
    ShopInfoDao shopInfoDao;
    @Autowired
    ShopManagerDao shopManagerDao;
    @Autowired
    ShopManagerShopDao shopManagerShopDao;
    @Autowired
    ParkInfoDao parkInfoDao;


    @Transactional
    @Override
    public void add(ShopInfoAddForm form) {

        ShopInfo shopInfo = new ShopInfo();
        BeanUtils.copyProperties(form,shopInfo);
        String geoHashBinary = GeoHashUtil.getBinary(Double.parseDouble(form.getLongitude()), Double.parseDouble(form.getLatitude()), 32);
        shopInfo.setGeohash(GeoHashUtil.getBase32(geoHashBinary));
        shopInfo.setPassword("000000");
        shopInfo.setStatus(1);
        shopInfo.setIsDeleted(0);
        shopInfoDao.insert(shopInfo);

        ShopManagerDTO manager = shopManagerDao.getManagerByTelephone(form.getTelephone());
        if (manager == null) {
            ShopManager shopManager = new ShopManager();
            shopManager.setTelephone(form.getTelephone());
            shopManager.setUsername(form.getUsername());
            shopManager.setPassword("000000");
            shopManager.setIsShopkeeper(1);
            shopManager.setIsDeleted(0);
            shopManagerDao.insert(shopManager);

            ShopManagerShop sms = new ShopManagerShop();
            sms.setManagerId(shopManager.getId());
            sms.setShopId(shopInfo.getId());
            sms.setIsDeleted(0);
            shopManagerShopDao.insert(sms);
        }

    }

    @Override
    public IPage<ShopInfoDTO> getShopInfoPage(ShopInfoSelectForm form) {
        Page<Object> page = new Page<>(form.getCurrentPage(), form.getPageSize());
        IPage<ShopInfoDTO> ipage = shopInfoDao.getShopInfoPage(page,form);
        if (ipage != null && ipage.getRecords() != null && ipage.getRecords().size() > 0) {
            for (ShopInfoDTO shopInfoDTO : ipage.getRecords()) {
                List<ParkInfoDTO> parkInfoDTOS = parkInfoDao.getNearbyParKByCoordinate(shopInfoDTO.getLongitude(), shopInfoDTO.getLatitude());
                shopInfoDTO.setParkList(parkInfoDTOS);
            }
        }

        return ipage;
    }

    @Override
    public ShopInfoDTO getShopById(Long shopId) {
        ShopInfoDTO shopInfoDTO = shopInfoDao.getshopInfoById(shopId);
        return shopInfoDTO;
    }

    @Transactional
    @Override
    public void updateShopInfo(ShopInfoUpdateForm form) {

        ShopInfo shopInfo = new ShopInfo();
        BeanUtils.copyProperties(form,shopInfo);
        String geoHashBinary = GeoHashUtil.getBinary(Double.parseDouble(form.getLongitude()), Double.parseDouble(form.getLatitude()), 32);
        shopInfo.setGeohash(GeoHashUtil.getBase32(geoHashBinary));
        shopInfo.setStatus(1);
        shopInfo.setIsDeleted(0);
        shopInfoDao.updateById(shopInfo);
    }

    @Override
    public List<ShopInfoDTO> shopInfoList() {
        return shopInfoDao.shopInfoList();
    }

    @Override
    public List<ShopInfoDTO> getNoManager() {
        return shopInfoDao.getNoManager();
    }
}
